<template>
  <div>
    <el-dialog title="优惠券列表" width="60%" v-model="dialogTableVisible">
      <div>
        <el-form :inline="true" @submit.prevent>
          <el-form-item label="券名称">
            <el-input
              v-model="query.couponName"
              placeholder="请输入券名称"
              @keyup.enter="currentChange(1)"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="currentChange(1)"
              >查询</el-button
            >
          </el-form-item>
          <el-form-item>
            <el-button type="success" icon="Check" @click="submitForm"
              >保存
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-table
          ref="couponTable"
          :data="tableData"
          row-key="couponId"
          border
          default-expand-all
          stripe
          @select="handleSelection"
        >
          <el-table-column type="selection"></el-table-column>
          <el-table-column type="index" label="序号" width="60">
          </el-table-column>
          <el-table-column prop="couponName" label="券名称" width="200">
          </el-table-column>
          <el-table-column
            prop="couponType"
            :formatter="getCouponType"
            label="券类型"
          >
          </el-table-column>
          <el-table-column prop="couponAmount" label="券面额">
          </el-table-column>
          <el-table-column prop="couponLimit" label="使用门槛">
          </el-table-column>
        </el-table>
        <el-row type="flex" class="row-bg" justify="end">
          <el-pagination
            background
            layout="total, prev, pager, next"
            :current-page="query.current"
            :page-size="query.size"
            :total="query.total"
            @current-change="currentChange"
          >
          </el-pagination>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="CouponTable">
import { pageCoupon } from "../../api/coupon";
import { listDictValueByDictCode } from "@/api/dictValue";
import { ref, onMounted } from "vue";

let emit = defineEmits(["onClick"]);
let dialogTableVisible = ref(false);
let couponTable = ref();
let tableData = ref([]);
let query = ref({ size: 10, current: 0, total: 1 });
let selectionCoupons = ref([]);
let couponTypeOptions = ref([]);

function getCouponType(row) {
  let couponType = couponTypeOptions.value.filter(
    (couponType) => couponType.value == row.couponType
  );
  if (couponType) {
    return couponType[0].label;
  }
  return "";
}
function loadDictValue() {
  listDictValueByDictCode("coupon_type")
    .then((res) => {
      couponTypeOptions.value = res.data;
      couponTypeOptions.value.forEach(function (obj) {
        obj.value = parseInt(obj.value);
      });
    })
    .catch((err) => {
      console.log(err);
    });
}
function loadData() {
  pageCoupon(query.value)
    .then((res) => {
      query.value.size = res.data.size;
      query.value.current = res.data.current;
      query.value.total = res.data.total;
      tableData.value = res.data.records;
    })
    .catch((err) => {
      console.log(err);
    });
}
function currentChange(current) {
  query.value.current = current;
  loadData();
}
function handleSelection(selection, row) {
  selectionCoupons.value = selection;
}
function submitForm() {
  couponTable.value.clearSelection();
  emit("onClick", selectionCoupons.value);
}

defineExpose({ dialogTableVisible });
onMounted(() => {
  loadDictValue();
  loadData();
});
</script>

<style scoped></style>
